/*
 * Copyright 2012 Focus Technology, Co., Ltd. All rights reserved.
 */
package com.coderonrails.cdistr.master.comint;

import java.net.ServerSocket;
import java.net.Socket;

/**
 * ServerThread.java
 * 
 * @author chenjianguo
 */
public abstract class ServerThread implements Runnable {
    private ServerSocket serverSocket = null;
    private MasterServices masterServer;

    public ServerThread() {
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        try {
            while (masterServer.isRunning()) {
                Socket socket = serverSocket.accept();
                doProcess(socket);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                serverSocket.close();
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public abstract void doProcess(Socket socket);

    public ServerSocket getServerSocket() {
        return serverSocket;
    }

    public void setServerSocket(ServerSocket serverSocket) {
        this.serverSocket = serverSocket;
    }

    public MasterServices getMasterServer() {
        return masterServer;
    }

    public void setMasterServer(MasterServices masterServer) {
        this.masterServer = masterServer;
    }
}
